package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Content;
import com.jiawa.wiki.domain.Doc;
import com.jiawa.wiki.domain.DocExample;
import com.jiawa.wiki.mapper.ContentMapper;
import com.jiawa.wiki.mapper.DocMapper;
import com.jiawa.wiki.req.DocQueryReq;
import com.jiawa.wiki.req.DocSaveReq;
import com.jiawa.wiki.resp.DocQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.util.CopyUtil;
import com.jiawa.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-05-16 11:14
 */
@Service
public class DocService {

    private static final Logger LOG = LoggerFactory.getLogger(DocService.class);

    @Resource
    private DocMapper docMapper;

    @Resource
    private ContentMapper contentMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 一次性查询所有分类
     *
     * @return
     */
    public List<DocQueryResp> all() {
        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort ASC");
        List<Doc> docList = docMapper.selectByExample(docExample);

        // 数据脱敏转移大法，CopyUtil 列表复制
        List<DocQueryResp> list = CopyUtil.copyList(docList, DocQueryResp.class);

        return list;
    }

    /**
     * 查询分类列表
     *
     * @param req
     * @return
     */
    public PageResp<DocQueryResp> list(DocQueryReq req) {
        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort ASC");
        // 相当于 sql where条件
        DocExample.Criteria criteria = docExample.createCriteria();
        // 传入名称不为空，才拼接查询条件
//        String bookName = req.getName();
//        if (!ObjectUtils.isEmpty(bookName)) {
//            criteria.andNameLike("%" + bookName + "%");
//        }

        // PageHelper调用startPage()方法之后，执行的第一个select语句会被分页
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Doc> docList = docMapper.selectByExample(docExample);

        PageInfo<Doc> pageInfo = new PageInfo<>(docList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 数据脱敏转移，CopyUtil 列表复制
        List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);

        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);

        return pageResp;
    }

    /**
     * 保存分类 -> 新增 OR 更新
     */
    public void save(DocSaveReq req) {

        // 数据转移
        Doc doc = CopyUtil.copy(req, Doc.class);
        Content content = CopyUtil.copy(req, Content.class);

        // 主键无数值，新增；主键有数值，更新
        if (ObjectUtils.isEmpty(req.getId())) {
            // 新增
            doc.setId(snowFlake.nextId());
            docMapper.insertSelective(doc);

            content.setId(doc.getId());
            contentMapper.insertSelective(content);
        } else {
            // 更新
            docMapper.updateByPrimaryKey(doc);
            int count = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (count == 0) {
                contentMapper.insertSelective(content);
            }
        }
    }

    /**
     * 删除分类
     */
    public void delete(Long id) {

        docMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据主键集合，删除分类
     */
    public void delete(List<String> ids) {

        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        // List<String> 转 List<Long>
        List<Long> idLongList = ids
                .stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());
        criteria.andIdIn(idLongList);
        docMapper.deleteByExample(docExample);
    }

    /**
     * 查询文档内容
     */
    public String findContent(Long id) {

        Content content = contentMapper.selectByPrimaryKey(id);
        return content.getContent();
    }

}
